#include<stdio.h>
#include<stdlib.h>
int k;
struct node *add_list(struct node *list, int n);
struct node
{
	int value;
	struct node *next;
};
struct node *start;

void count1(struct node *list)
{
	
	int i,k=0,c=0;	
	printf("\nenter element you want to count: ");
	scanf("%d", &k);
	for(;list!=NULL;list=list->next)
	{
		if(list->value==k)
			c++;
	}
 	printf("\nNo. of occurences = %d",c);
}
void append(struct node *list, int n)
{
	struct node *new;	
	new = malloc(sizeof(struct node));
	if(new==NULL)
	{
		printf("failure");
		exit(EXIT_FAILURE);
	}
	else
	{
		for(; list->next!=NULL;list=list->next)
		;
		new->next=NULL;
		new->value=n;
		list->next=new;
		//list=list->next;
	}
	
}
void extend()
{
	int i,k,x;
	printf("enter no. of elements you want to extend: ");
	scanf("%d", &k);
	for (i=1; i<=k; i++)
	{
		printf("\nenter element %d:", i);
		scanf("%d", &x);
		append(start,x);
	}
	
}

void index(struct node *list)
{
	int i,e,pos;
	printf("\nenter element you want to search: ");
	scanf("%d",&e);
	for(;list!=NULL;list=list->next)
	{
		pos++;
		if(list->value==e)
		{
			break;
		}
	}
	printf("first occurence found at node:%d ", pos);
}

void sort(struct node *list)
{
	struct node *i, *j, *p;
	int temp,flag,k;
	for(i=list;(i->next)!=NULL; i=i->next)
	{
		k=i->value;
		for(j=i->next;j!=NULL; j=j->next)
		{
			if(j->value<k)
			{
				p=j;
				k=j->value;
				flag=1;
			}
		}
		if(flag==1)
		{
			temp=i->value;
			i->value=k;
			p->value=temp;
		}
		flag=0;
	}
}

void insert(struct node *list)
{
	int n,pos,i;
	printf("\nenter value of new element: ");
	scanf("%d", &n);
	printf("\nFirst position denoted by 1 second by 2 and so on...");
	printf("\nenter position where you want to insert: ");
	scanf("%d", &pos);
	struct node *new,*p;
	new =(struct node*) malloc(sizeof(struct node));
	if(new==NULL)
	{
		printf("memory allocation failure");
		exit(EXIT_FAILURE);
	}
	else
	{
		new->value=n;
		for(i=1;i<pos-1;i++)
		{
			list=list->next;
		}
		p=list->next;
		list->next=new;
		new->next=p;
	}
}

void remove1(struct node **list)
{
	int e;
	printf("\nenter element you want to delete: ");
	scanf("%d",&e);
	struct node *p=*list,*q=NULL;
	  
	while(p!= NULL && p->value!=e)
	{
	 	q=p;
		p=p->next;
	}
	if (p==NULL) 
   		printf("Value not found"); 
	if (q==NULL)
	{
		*list=(*list)->next;
		free(p);
	}
	else
	{
		q->next=p->next;
		free(p);
	}
}

void index1(struct node *list)
{
	int pos,e,c=0;
	printf("\nenter element you want to search: ");
	scanf("%d",&e);
	for(;list!=NULL;list=list->next)
	{
		c++;		
		if(list->value==e)
		{
			printf("\nElement found at node:%d",c);
			break;
		}
	}
}
	
void reverse1(struct node **list)
{
    	struct node *temp,*prev=NULL,*l=*list,*p=*list;
	printf("\nold linked list: ");
	for(;p!=NULL;p=p->next)
	{
		printf("%d ",p->value);
	}	
    	while(l!= NULL)
	{
        	temp=l->next;
        	l->next=prev;
        	prev=l;
        	l=temp;
    	}
	
	*list=prev;
}
		
	
	
struct node * create(struct node *list, int n)
{ 
	struct node *new;
	
	new = malloc(sizeof(struct node));
	if(new==NULL)
	{
		printf("memory allocation failure");
		exit(EXIT_FAILURE);
	}
	else
	{
		new->value=n;
		new->next=list;
		return new;
	}
}
void display(struct node *list)
{
	printf("\nmodified list: ");	
	for(;list!=NULL;list=list->next)
	{
		printf("%d ",list->value);
	}
}

void main(void)
{
	int i,n,ch;
	printf("enter no. of nodes: ");
	scanf("%d", &k);
	struct node *first=NULL;
	struct node *second=NULL;
	struct node *p;
	for (i=1;i<=k;i++)
	{
		printf("Enter element %d: ", i);
		scanf("%d", &n);
		first=create(first,n);
	}
	for (p=first;p!=NULL;p=p->next)
	{
		second=create(second, p->value);
		
	}
	start=second;
	printf("\n");
	
	while(ch!=0)
	{
		printf("\nPress 1 for count");
		printf("\nPress 2 for append");	
		printf("\nPress 3 for sort");	
		printf("\nPress 4 for insert");	
		printf("\nPress 5 for remove");	
		printf("\nPress 6 for index");	
		printf("\nPress 7 for reverse");
		printf("\nPress 8 for extend");	
		printf("\nPress 9 t0 print the current linked list");
		printf("\nPress 0 to exit");
		
		printf("\nEnter your choice: ");
		scanf("%d" ,&ch);
	
		switch (ch)
		{
			case 1:
			{
				count1(start);
				break;
			}
			case 2:
			{
				int n;
				printf("\nenter value of new element: ");
				scanf("%d", &n);
				append(start,n);
				display(start);
				break;
			}
			case 3:
			{
				sort(start);
				display(start);
				break;
			}
			case 4:
			{
				insert(start);
				display(start);
				break;
			}
			case 5:
			{
				remove1(&start);
				display(start);
				break;
			}
			case 6:
			{
				index1(start);
				break;
			}
			case 7:
			{
				reverse1(&start);
				display(start);
				break;
			}
			case 8:
			{
				extend();
				display(start);
				break;
			}
			case 9:
			{
				display(start);
				break;
			}
			case 0:
			{
				break;
			}
				
			default:
			{
				printf("option not available");
			
			}
		}
	}
}

















